/*
 * author: wangx
 */
 $(document).ready(function(){
   removeNav();
 });
document.getElementsByClassName('homeUserName')[0].innerText = localStorage.realName;
debugger
/* 获取近期安排 */
var allSummary = [];
var requestUrl = hostUrl + '/record/getrecent';
requestPlanList();

function requestPlanList() {
  var el = {};
  el.username = localStorage.session;
  requestWithSession(el, requestUrl, function(data) {
      var container = document.getElementsByClassName('rentPlanList')[0];
      var planStr = '';
      var planData = data.data;
      for (var i = 0; i < planData.length; i++) {
        planStr += "<span class='planOrder'>" + planData[i].level +
          "</span><span class='planTime smallDate'>" + planData[i].startdate +
          "-" + planData[i].enddate + "</span><span class='planContain'>" +
          planData[i].content + "</span><span class='planOperate'><button class='deletePlanBtn' value='" + planData[i].id + "' onclick='deleteRencentPlan(this.value)'>删除</button></span>"
      }
      container.innerHTML = planStr;
    },
    function(error) {
      console.log(error);
    });
}

/* 删除近期安排 */
function deleteRencentPlan(id) {
  var delReqUrl = hostUrl + '/record/deleterecent';
  var el = {};
  el.id = id;
  requestWithSession(el, delReqUrl, function(data) {
      requestPlanList();
    },
    function(error) {
      console.log(error);
    });
}

/* 添加近期安排 */
function addPlanBtn() {
  var stdate = document.getElementById('startDate').value;
  var edate = document.getElementById('endDate').value;
  var planContent = document.getElementById('planContent').value;
  var select = document.getElementById('selectPlanLevel');
  var index = select.selectedIndex;
  var level = Number(select.options[index].text);
  var el = {};
  var reqUrl = hostUrl + '/record/addrecent';
  el.username = localStorage.session;
  el.content = planContent;
  el.startdate = stdate;
  el.enddate = edate;
  el.level = level;
  requestWithSession(el, reqUrl, function(data) {
      requestPlanList();
    },
    function(error) {
      console.log(error);
    });
}

/* 获取个人物资 */
requestPersonalResource();

function requestPersonalResource() {
  var el = {};
  el.username = localStorage.session;
  var reqUrl = hostUrl + '/resource/getlist';
  requestWithSession(el, reqUrl, function(data) {
      var reStr = '',
        reList = data.data,
        box = document.getElementsByClassName('mySuppliesItemBox')[0];

      for (var i = 0; i < reList.length; i++) {
        var bdate = new Date(reList[i].borrowdate),
          rdate = new Date(reList[i].returndate);
        reStr += "<span class='sourceOrder'>" + reList[i].resourcename +
          "</span><span class='sourceOwner'>" + reList[i].content +
          "</span><span class='sourcePower'>" + bdate.getFullYear() + '-' + bdate.getMonth() + '-' + bdate.getDate() +
          "</span><span class='sourceOperate'>" + rdate.getFullYear() + '-' + rdate.getMonth() + '-' + rdate.getDate() + "</span>";
      }
      box.innerHTML = reStr;
    },
    function(error) {
      console.log(error);
    });
}
/*获取闲置物资列表*/
requestUnUsedResourceList();

function requestUnUsedResourceList() {
  var el = {};
  var reqUrl = hostUrl + '/resource/unusedresourcelist';
  requestWithSession(el, reqUrl, function(data) {
      var list = data.data;
      var box = document.getElementsByClassName('unUsedResouceItemBox')[0];
      var str = '';
      for (var i = 0; i < list.length; i++) {
        str += "<span class='unUsedResouceId'>" + list[i].resourceid +
          "</span><span class='unUsedResouceName'>" + list[i].resourcename +
          "</span><span class='unUsedResouceContent'>" + list[i].content +
          "</span><span class='unUsedResouceApply'><button value='" + list[i].resourceid +
          "' onclick='showApplyUnUsedBox(this.value)'>申请</button></span>"
      }
      box.innerHTML = str;
    },
    function(error) {
      console.log(error);
    });
}

function showApplyUnUsedBox(id) {
  document.getElementById('applyUnUsedResourceId').innerHTML = id;
  document.getElementsByClassName('showApplyUnUsedBox')[0].style.display = 'block';
}

function closeApplyUnUsedBox() {
  document.getElementsByClassName('showApplyUnUsedBox')[0].style.display = 'none';
}
/*  申请物资 */
function applyUnUsedResource(id) {
  var el = {};
  el.resourceid = id;
  el.returndate = document.getElementById('applyReturnDate').value;
  var reqUrl = hostUrl + '/resource/apply';
  requestWithSession(el, reqUrl, function(data) {
      if(data.status === 0){
        alert('申请成功！');
      }else {
        alert('申请失败!');
      }
      closeApplyUnUsedBox();
    },
    function(error) {
      console.log(error);
    });
}
/* 获取周总结 */
requsetAllWeekSummary();

function requsetAllWeekSummary() {
  var el = {};
  var reqUrl = hostUrl + '/record/getsummary';
  el.username = localStorage.session;
  requestWithSession(el, reqUrl, function(data) {
    allSummary = data.data;
    console.log(allSummary);
    var sumStr = '';
    var termStr = ["2016秋", "2017春", "2017秋", "2017春", "2018秋", "2019春"];
    for (var i = 0; i < allSummary.length; i++) {
      sumStr += "<span class='semester'>" + termStr[allSummary[i].term] +
        "</span><span class='weekCount'>" + allSummary[i].week +
        "</span><span class='weekSum'>" + allSummary[i].title +
        "</span><span class='view'><button value='" + allSummary[i].id +
        "' onclick='showViewSummary(this.value)'>查看</button></span>";
    }
    document.getElementsByClassName('weekSummaryContainer')[0].innerHTML = sumStr;
  }, function(error) {
    console.log(error);
  });
}

function showViewSummary(id) {
  var box = document.getElementsByClassName('viewUserWeekSummaryContent')[0];
  document.getElementsByClassName('viewUserWeekSummaryBox')[0].style.display = 'block';
  var showSumStr = '';
  for (var i = 0; i < allSummary.length; i++) {
    if (allSummary[i].id == id) {
      showSumStr += "<h4>" + allSummary[i].title + "</h4><p>" + allSummary[i].content + "</p>";
    }
  }
  box.innerHTML = showSumStr;
}

function closeViewSummary() {
  document.getElementsByClassName('viewUserWeekSummaryBox')[0].style.display = 'none';
}

function submitSummary() {
  var title = document.getElementById('summaryTitle').value,
    content = document.getElementById('summaryText').value,
    term = document.getElementById('inputSemeter').value,
    week = document.getElementById('inputWeekCount').value;
  var reqUrl = hostUrl + '/record/addsummary';
  var el = {};
  el.username = localStorage.session;
  el.title = title;
  el.content = content;
  el.term = term;
  el.week = week;
  requestWithSession(el, reqUrl, function(data) {
      requsetAllWeekSummary();
    },
    function(error) {
      console.log(error);
    });
}
